from http import HTTPStatus
from dashscope import Generation
import json

# pip install dashscope
# Get a new token: https://help.aliyun.com/document_detail/611472.html?spm=a2c4g.2399481.0.0
# For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html

key = 'sk-af7d53a016f744e586432e291685df54'

def call_with_messages():
    messages = [{'role': 'system', 'content': '你是达摩院的生活助手机器人。'}, 
                {'role': 'user', 'content': '如何做西红柿鸡蛋？'}]
    gen = Generation()
    response = gen.call(
        Generation.Models.qwen_turbo,
        messages=messages,
        api_key=key,
        result_format='message', # set the result is message format.
    )
    if response.status_code == HTTPStatus.OK:   
        print(response)  
    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s'%(
            response.request_id, response.status_code, 
            response.code, response.message
        ))     

def call_with_prompt():


    response=Generation.call(
        model='qwen-turbo',
        prompt='如何做西红柿炖牛腩？',
        api_key=key,
        )

    if response.status_code==HTTPStatus.OK:
        print(json.dumps(response.output, indent=4, ensure_ascii=False))
    else:
        print('Code: %d, status: %s, message: %s' % (response.status_code, response.code, response.message))   

if __name__ == '__main__':
    # call_with_messages()
    call_with_prompt()